﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

using NGE;

namespace MH.SE
{
    public partial class TilePicker : UserControl
    {
        List<FileIndexer> fileList = new List<FileIndexer>();
        Image img;
        Size tileSize;
        Rectangle selectedRect;
        int fileIndex;
        Color fcolor;
        public delegate void OnSelected();
        public event OnSelected OnFileSelected;
        public event OnSelected OnRectSelected;

        public Rectangle SelectRect
        {
            get
            {
                return selectedRect;
            }
        }
        public FileIndexer SelectFile
        {
            get
            {
                if (fileIndex < 0)
                {
                    return null;
                }
                return fileList[fileIndex];
            }
        }
        public Color SelectColor
        {
            get
            {
                return fcolor;
            }
        }
        public Size TileSize
        {
            get
            {
                return tileSize;
            }
        }

        public TilePicker()
        {
            InitializeComponent();
        }

        private void TilePicker_Load(object sender, EventArgs e)
        {
            tileSize = new Size(16, 16);
            fileIndex = -1;
            fcolor = Color.White;
            OnFileSelected = new OnSelected(not);
            OnRectSelected = new OnSelected(not);
        }
        void not()
        { }

        string rp;
        public void GetFileList(string rootpath)
        {
            rp = rootpath;
            comboBox1.Items.Clear();
            List<string> dirList = new List<string>();
            NGE.Util.EnumDir(rootpath, ref dirList);

            foreach (string str in dirList)
            {
                NGE.Util.EnumFile(str, "*.png", ref fileList);
            }

            foreach (FileIndexer fi in fileList)
            {
                comboBox1.Items.Add(fi.FileName);
            }
        }
        void GetFileList(string rootpath, string filter)
        {
            comboBox1.Items.Clear();
            fileList.Clear();

            List<string> dirList = new List<string>();
            NGE.Util.EnumDir(rootpath, ref dirList);

            foreach (string str in dirList)
            {
                NGE.Util.EnumFile(str, "*.png", ref fileList);
            }

            List<int> ilist=new List<int>();
            for (int i = 0; i < fileList.Count; i++)
            {
                if (fileList[fileList.Count - 1 - i].FileName.Contains(filter) == false)
                {
                    ilist.Add(fileList.Count - 1 - i);
                }
            }
            foreach (int i in ilist)
            {
                fileList.RemoveAt(i);
            }

            foreach (FileIndexer fi in fileList)
            {
                comboBox1.Items.Add(fi.FileName);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                GetFileList(rp, textBox5.Text);
                tileSize.Width = int.Parse(textBox7.Text);
                tileSize.Height = int.Parse(textBox6.Text);

                int a, r, g, b;
                a = int.Parse(textBox1.Text);
                r = int.Parse(textBox2.Text);
                g = int.Parse(textBox4.Text);
                b = int.Parse(textBox3.Text);
                fcolor = Color.FromArgb(a, r, g, b);

            }
            catch { }
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            pictureBox1.Refresh();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            fileIndex = comboBox1.SelectedIndex;
            if (fileIndex < 0)
            {
                return;
            }
            img = Image.FromFile(fileList[fileIndex].FullPathAndFileName);
            pictureBox1.Size = img.Size;
            pictureBox1.Refresh();
            OnFileSelected();
        }
        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;

            if(img!=null)
            g.DrawImage(img, new Point());

            if (checkBox1.Checked)
            {
                for (int y = 0; y <= img.Height; y += tileSize.Height)
                {
                    g.DrawLine(Pens.Blue, 0, y, img.Width, y);
                }
                for (int x = 0; x <= img.Width; x += tileSize.Width)
                {
                    g.DrawLine(Pens.Blue, x, 0, x, img.Height);
                }
            }

            g.DrawRectangle(Pens.Red, selectedRect);
        }

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            selectedRect.Size = new Size();

            Point p = e.Location;
            p.X /= tileSize.Width;
            p.Y /= tileSize.Height;
            p.X *= tileSize.Width;
            p.Y *= tileSize.Height;

            selectedRect.Location = p;
            pictureBox1.Refresh();
        }

        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            Point p = e.Location;
            p.X /= tileSize.Width;
            p.Y /= tileSize.Height;
            p.X++;
            p.Y++; 
            p.X *= tileSize.Width;
            p.Y *= tileSize.Height;

            selectedRect.Size = new Size(p.X - selectedRect.X, p.Y - selectedRect.Y);

            pictureBox1.Refresh();

            OnRectSelected();
        }

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                Point p = e.Location;
                p.X /= tileSize.Width;
                p.Y /= tileSize.Height;
                p.X++;
                p.Y++;
                p.X *= tileSize.Width;
                p.Y *= tileSize.Height;

                selectedRect.Size = new Size(p.X - selectedRect.X, p.Y - selectedRect.Y);

                pictureBox1.Refresh();
                OnRectSelected();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            selectedRect.Location = new Point();
            selectedRect.Size = img.Size;
            pictureBox1.Refresh();
            OnRectSelected();
        }
    }
}
